<% layout('/layouts/default.html', {title: '预收转账单管理', libs: ['validate','dataGrid']}){ %>
<div class="main-content">
	<div class="box box-main">
		<div class="box-header with-border">
			<div class="box-title">
				<i class="fa icon-note"></i> ${text(tadvancesReceivedTransfer.isNewRecord ? '新增预收转账单' : '编辑预收转账单')}
			</div>
			<div class="box-tools pull-right">
				<button type="button" class="btn btn-box-tool" data-widget="collapse"><i class="fa fa-minus"></i></button>
			</div>
		</div>
		<#form:form id="inputForm" model="${tadvancesReceivedTransfer}" action="${ctx}/prerecitran/tadvancesReceivedTransfer/save" method="post" class="form-horizontal">
			<div class="box-body">
				<div class="form-unit">${text('基本信息')}</div>
				<#form:hidden path="advancesReceivedTransferId"/>
				<div class="row">
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required hide">*</span> ${text('编号')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:input path="advancesReceivedTransferCode" maxlength="100" class="form-control" placeholder="自动生成" readonly="true"/>
							</div>
						</div>
					</div>
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required hide">*</span> ${text('原预收款编号')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:select path="advancesReceivedCode"   items="${advancesReceivedList}" itemLabel="advancesReceivedCode" itemValue="advancesReceivedId" dictType="" class="form-control" />
							</div>
						</div>
					</div>
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required hide">*</span> ${text('客户')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:input path="customerId"  dictType="" class="form-control" readonly="true" />
							</div>
						</div>
					</div>
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required hide">*</span> ${text('服务订单')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:input path="serviceOrder"   class="form-control" readonly="true"/>
							</div>
						</div>
					</div>
				</div>
				<div class="row">
					<div class="col-xs-12">
						<div class="form-group">
							<label class="control-label col-sm-2" title="">
								<span class="required hide">*</span> ${text('备注信息')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-10">
								<#form:textarea path="remarks" rows="4" maxlength="500" class="form-control"/>
							</div>
						</div>
					</div>
				</div>

					<div class="col-sm-offset-2 col-sm-10">
						<div class="form-group">
							<button type="button" class="btn btn-primary btn-sm" id="searchDetail" style="${tadvancesReceivedTransfer.isNewRecord ? 'display:block':'display:none'}">${text('查询')}</button>
						</div>
					</div>

				<h4 class="form-unit">${text('预收转账单明细')}</h4>
				<div class="ml10 mr10">
					<table id="tadvancesReceivedTransferDetailDataGrid"></table>
				<!--	<% if (hasPermi('prerecitran:tadvancesReceivedTransfer:edit')){ %>
						<a href="#" id="tadvancesReceivedTransferDetailDataGridAddRowBtn" class="btn btn-primary btn-sm mt10 mb10"><i class="fa fa-plus"></i> ${text('增行')}</a>
					<% } %>-->
				</div>
				<h4 class="form-unit">${text('系统信息')}</h4>
				<div class="col-xs-6">
					<div class="form-group">
						<label class="control-label col-sm-4" title="">
							<span class="required hide">*</span> ${text('创建者')}：<i class="fa icon-question hide"></i></label>
						<div class="col-sm-8">
							<#form:input path="createBy" maxlength="64" class="form-control" disabled="true"/>
						</div>
					</div>
				</div>
				<div class="col-xs-6">
					<div class="form-group">
						<label class="control-label col-sm-4" title="">
							<span class="required hide">*</span> ${text('创建时间')}：<i class="fa icon-question hide"></i></label>
						<div class="col-sm-8">
							<#form:input path="createDate" readonly="true" maxlength="20" class="form-control" disabled="true"
							dataFormat="date" data-type="date" data-format="yyyy-MM-dd"/>
						</div>
					</div>
				</div>
				<div class="col-xs-6">
					<div class="form-group">
						<label class="control-label col-sm-4" title="">
							<span class="required hide">*</span> ${text('更新者')}：<i class="fa icon-question hide"></i></label>
						<div class="col-sm-8">
							<#form:input path="updateBy" maxlength="64" class="form-control" disabled="true"/>
						</div>
					</div>
				</div>
				<div class="col-xs-6">
					<div class="form-group">
						<label class="control-label col-sm-4" title="">
							<span class="required hide">*</span> ${text('更新时间')}：<i class="fa icon-question hide"></i></label>
						<div class="col-sm-8">
							<#form:input path="updateDate" readonly="true" maxlength="20" class="form-control" disabled="true"
							dataFormat="date" data-type="date" data-format="yyyy-MM-dd"/>
						</div>
					</div>
				</div>
			</div>
			<div class="box-footer">
				<div class="row">
					<div class="col-sm-offset-2 col-sm-10">
						<% if (hasPermi('prerecitran:tadvancesReceivedTransfer:edit')){ %>
							<button type="submit" class="btn btn-sm btn-primary" id="btnSubmit"><i class="fa fa-check"></i> ${text('保 存')}</button>&nbsp;
						<% } %>
						<button type="button" class="btn btn-sm btn-default" id="btnCancel" onclick="js.closeCurrentTabPage()"><i class="fa fa-reply-all"></i> ${text('关 闭')}</button>
					</div>
				</div>
			</div>
		</#form:form>
	</div>
</div>
<% } %>
<script>
//初始化预收转账单明细DataGrid对象
$("#tadvancesReceivedTransferDetailDataGrid").dataGrid({

	data: ${toJson(tadvancesReceivedTransfer.tadvancesReceivedTransferDetailList)},
	datatype: "local", // 设置本地数据
	autoGridHeight: function(){return 'auto'}, // 设置自动高度
	
	// 设置数据表格列
	columnModel: [
		{header:'状态', name:'status', editable:true, hidden:true},
		{header:'主键', name:'advancesReceivedTransferDetailId', editable:true, hidden:true},
		{header:'车辆', name:'carId', editable:true, hidden:true},
		{header:'${text("车辆")}', name:'carName', width:150, editable:true, edittype:'text', editoptions:{'maxlength':'64', 'class':'form-control','readonly':'true'}},
		{header:'${text("车架号")}', name:'frameNumber', width:150, editable:true, edittype:'text', editoptions:{'maxlength':'100', 'class':'form-control','readonly':'true'}},
		{header:'${text("原行号")}', name:'originRowNumber', width:150, editable:true, edittype:'text', editoptions:{'maxlength':'100', 'class':'form-control','readonly':'true'}},
		{header:'${text("原类型")}', name:'originalExpenseType', width:150, hidden:true, editable:true, edittype:'text', editoptions: {'class': 'form-control'}},
		{header:'${text("原费用类型")}', name:'originalExpenseTypes', width:150, editable:true, edittype:'text', editoptions:{'maxlength':'100', 'class':'form-control','readonly':'true'}},
		{header:'${text("可用余额")}', name:'advancesReceivedBalance', width:150, editable:true, edittype:'text', editoptions:{'class':'form-control number','readonly':'true'}},
		{header:'${text("费用类型")}', name:'expenseType', width:150, editable:true, edittype:'select', editoptions:{'class':'form-control',
			dataInit: function(element){
					$(element).select2().on("change",function(){});
				}
			}},
		{header:'${text("结转至车架号")}', name:'carryOverFrameNumber', width:150, editable:true, edittype:'select', editoptions:{'class':'form-control',
				dataInit: function(element){
					$(element).select2().on("change",function(){
						//var f =$("#prepaymentCode").val()
						var frame =$(this).val()
						var ser =$("#serviceOrder").val()
						var that =$(this);

						$.ajax({
							type: "POST",
							url: "${ctx}/prepay/tprepayment/findByMany1",
							datatype:"json",
							data: {
								fram :frame,
								row:"",
								ser:ser
							},
							success: function (data) {

								that.parent().parent().find("#rowNumber").find("option").each(function () {
									$(this).prop("selected",false)
									if($(this).html()==data.rowNumber){
										$(this).prop("selected",true)
									}
								})
								that.parent().next().find("span[class=\"select2-selection__rendered\"]").html(data.rowNumber)
								that.parent().next().find("span[class=\"select2-selection__rendered\"]").attr("title",data.rowNumber)
							}
						});
					});
				}
			}},
		{header:'${text("结转至行号")}', name:'rowNumber', width:150, editable:true, edittype:'select', editoptions:{'class':'form-control',
				dataInit: function(element){
					$(element).select2().on("change",function(){

						var row =$(this).val()
						var ser =$("#serviceOrder").val()
						var that =$(this);

						$.ajax({
							type: "POST",
							url: "${ctx}/prepay/tprepayment/findByMany1",
							datatype:"json",
							data: {
								fram :"",
								row:row,
								ser:ser
							},
							success: function (data) {
								that.parent().parent().find("#carryOverFrameNumber").find("option").each(function () {
									$(this).prop("selected",false)
									if($(this).html()==data.frameNumber){
										$(this).prop("selected",true)
									}
								})
								that.parent().prev().find("span[class=\"select2-selection__rendered\"]").html(data.frameNumber)
								that.parent().prev().find("span[class=\"select2-selection__rendered\"]").attr("title",data.frameNumber)
							}
						});
					});
				}
			}},
		{header:'${text("结转金额")}', name:'carryOverAmount', width:150, editable:true, edittype:'text', editoptions:{'class':'form-control number'}},
		{header:'${text("预收转账单ID")}', name:'advancesReceivedTransferId.advancesReceivedTransferId', editable:true, hidden:true},

	],
	
	// 编辑表格参数
	editGrid: true,				// 是否是编辑表格
	editGridInitRowNum: 0,		// 编辑表格的初始化新增行数
	editGridAddRowBtn: $('#tadvancesReceivedTransferDetailDataGridAddRowBtn'),	// 子表增行按钮
	editGridAddRowInitData: {advancesReceivedTransferDetailId: '', status: Global.STATUS_NORMAL},	// 新增行的时候初始化的数据
	
	// 编辑表格的提交数据参数
	editGridInputFormListName: 'tadvancesReceivedTransferDetailList', // 提交的数据列表名
	editGridInputFormListAttrs: 'originRowNumber,rowNumber,carId,status,advancesReceivedTransferDetailId,frameNumber,originalExpenseType,advancesReceivedBalance,expenseType,carryOverFrameNumber,carryOverAmount,advancesReceivedTransferId.advancesReceivedTransferId,', // 提交数据列表的属性字段
	
	// 加载成功后执行事件
	ajaxSuccess: function(data){
		
	}
});
</script>
<script>
$("#inputForm").validate({
	submitHandler: function(form){
		js.ajaxSubmitForm($(form), function(data){
			js.showMessage(data.message);
			if(data.result == Global.TRUE){
				js.closeCurrentTabPage(function(contentWindow){
					contentWindow.page();
				});
			}
		}, "json");
    }
});


	$(function () {
		//查询
		$("#searchDetail").click(function () {
			var advancesReceived=$("#advancesReceivedCode").val()
			$.ajax({
				type: "post",
				url: "${ctx}/prerecitran/tadvancesReceivedTransfer/getPreReic",
				data: {id:advancesReceived},
				dataType: "json",
				success: function(data){
					$('#tadvancesReceivedTransferDetailDataGrid').jqGrid('clearGridData');　　　　//先清空，再加载
					for(var i=0;i<data.tadvancesReceivedEstimatedList.length;i++){

						$("#tadvancesReceivedTransferDetailDataGrid").jqGrid("addRow", {
							position: "last",
							addRowParams: {keys: false, focusField: true},
							initdata: {id: "",
								frameNumber: data.tadvancesReceivedEstimatedList[i].frameNumber,
								originalExpenseType:data.tadvancesReceivedEstimatedList[i].advancesReceivedType,
								originalExpenseTypes:data.tadvancesReceivedEstimatedList[i].advancesReceivedTypeName,
								advancesReceivedBalance:data.tadvancesReceivedEstimatedList[i].usedAmount,
								carName:data.tadvancesReceivedEstimatedList[i].carName,
								carId:data.tadvancesReceivedEstimatedList[i].carId,
								originRowNumber:data.tadvancesReceivedEstimatedList[i].rowNumber,
							}
						})
					}
					getFrameNumber()
					getRowNumber()
				}

			})

		})


		//下拉列表框change
		$("#advancesReceivedCode").change(function () {
			var id =$(this).val();
			$.ajax({
				type: "post",
				url: "${ctx}/prerecitran/tadvancesReceivedTransfer/getPreReic",
				data: {id:id},
				dataType: "json",
				success: function(data){
					$("#customerId").val(data.customer.customerName)
					$("#serviceOrder").val(data.serviceOrder.serviceOrderCode)
				}
			});
		})
	})

//直接执行一次
	$(function () {

		var id =$("#advancesReceivedCode").val();

		$.ajax({
			type: "post",
			url: "${ctx}/prerecitran/tadvancesReceivedTransfer/getPreReic",
			data: {id:id},
			dataType: "json",
			success: function(data){

				$("#customerId").val(data.customer.customerName)
				$("#serviceOrder").val(data.serviceOrder.serviceOrderCode)
				if("${!tadvancesReceivedTransfer.isNewRecord}"){
					getFrameNumber()
					getRowNumber()
				}
			}
		});
	})


	//获得车架号
function getFrameNumber(){

	var str = ''
	if ($("#serviceOrder").val() != "") {
		$.ajax({
			type: "POST",
			url: "${ctx}/payment/tap/getFrameNumberList2",
			data: {
				"serviceOrderId": $("#serviceOrder").val(),
			},
			success: function (data) {

				if (data!= "") {

					//根据订单编号赋值车架号
					if (data.length > 0) {

						for (var i = 0; i < data.length-1; i++) {
							var frameNumber1 = data[i].frameNumber;
								if(frameNumber1!=null&&frameNumber1!=""){
									str+=frameNumber1+":"+frameNumber1+";";
								}
							}
						}
					if(str.length>1){
						str=str.substring(0,str.length-1);
					}
				}
				$('#tadvancesReceivedTransferDetailDataGrid').setColProp('carryOverFrameNumber', {editoptions: {value:str,readOnly: true}});
				$('#tadvancesReceivedTransferDetailDataGrid').dataGrid('reloadGrid');
			}
		});
	} else{
		$('#tadvancesReceivedTransferDetailDataGrid').setColProp('carryOverFrameNumber', {editoptions: {value:"",readOnly: true}});
		$('#tadvancesReceivedTransferDetailDataGrid').dataGrid('reloadGrid');
	}
}
//获得行号
function getRowNumber(){

	var str = ''
	if ($("#serviceOrder").val() != "") {
		$.ajax({
			type: "POST",
			url: "${ctx}/payment/tap/getNumberList",
			data: {
				"serviceOrderId": $("#serviceOrder").val(),
			},
			success: function (data) {

				if (data!= "") {
					//根据订单编号赋值车架号
					if (data.length > 0) {

						for (var i = 0; i < data.length; i++) {
							var frameNumber1 = data[i];


								if(i!=data.length-1){
									str+=frameNumber1+":"+frameNumber1+";";
								}else {
									str+=frameNumber1+":"+frameNumber1;// 这里是option里面的 value:label
								}

						}

					}
				}
				$('#tadvancesReceivedTransferDetailDataGrid').setColProp('rowNumber', {editoptions: {value:str,readOnly: true}});
				$('#tadvancesReceivedTransferDetailDataGrid').dataGrid('reloadGrid');
			}
		});
	} else{
		$('#tadvancesReceivedTransferDetailDataGrid').setColProp('rowNumber', {editoptions: {value:"",readOnly: true}});
		$('#tadvancesReceivedTransferDetailDataGrid').dataGrid('reloadGrid');
	}
}
//给每一个转账金额添加blur事件
$(function () {
	$("table").on("blur","input[name=\"carryOverAmount\"]",function () {
		var m =$(this).val();
		var m1=$(this).parent().parent().find("input[name=\"advancesReceivedBalance\"]").val();

		var flag =/^[0-9]*(\.[0-9]*)?$/;
		if(!flag.test(m)){
			//js.showMessage(data.message);
			return;
		}
		if(parseFloat(m)>parseFloat(m1)){
			js.showMessage("请输入正确金额");
			$(this).val("");
			return;
		}

	})
})

//费用项目,页面加载完调用
$(function () {
	var str="";
	$.ajax({
		type: "POST",
		async : false,
		url: "${ctx}/payment/tap/getExpenseItem",

		dataType : "json",
		success: function (data) {
			// $('#currency').select2('val', data.currency);
			//根据订单编号赋值车架号
			if (data.length > 0) {
				for (var i = 0; i < data.length; i++) {
					var frameNumber1 = data[i].fixedExpenseName;

					var frameNumberId = data[i].fixedExpenseId;
					if(i!=data.length-1){
						str+=frameNumberId+":"+frameNumber1+";";
					}else {
						str+=frameNumberId+":"+frameNumber1;// 这里是option里面的 value:label
					}
				}

			} else{
				js.showErrorMessage("该订单没有信息！");
			}
			$('#tadvancesReceivedTransferDetailDataGrid').setColProp('expenseType', {editoptions: {value:str}});
			$('#tadvancesReceivedTransferDetailDataGrid').dataGrid('reloadGrid');

		}
	});

})


	//车架号、行号联动设置
	$(function () {

		//车架号
		$("#tadvancesReceivedTransferDetailDataGrid").on("change","select[name=\"carryOverFrameNumber\"]",function () {

		})

		//行号
		$("#tadvancesReceivedTransferDetailDataGrid").on("change","select[name=\"rowNumber\"]",function () {

			var f =$("#prepaymentCode").val()
			//var row =$("#rownumber").val()
			var ser =$("#serviceOrder").val()

			$.ajax({
				type: "POST",
				url: "${ctx}/prepay/tprepayment/findByMany",
				datatype:"json",
				data: {
					fram :f,
					row:"",
					ser:ser
				},
				success: function (data) {

					$("#rownumber").find("option").each(function () {
						$(this).prop("selected",false)
						if($(this).html()==data.rowNumber){
							$(this).prop("selected",true)
						}
					})
					$("#select2-rowNumber-container").html(data.rowNumber)
					$("#select2-rowNumber-container").attr("title",data.rowNumber)
					$("#carnames").val(data.tcar.carName)
				}
			});

		})
	})


</script>